/*
 * 文件名：SystemDaoImpl.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-18
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.dao.impl;


import java.sql.Types;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qbgs.rbac.dao.SystemDao;


/**
 * 
 * @author will_awoke
 * @version 2014-8-18
 * @see SystemDaoImpl
 * @since
 */
@Repository
public class SystemDaoImpl implements SystemDao
{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int queryByLoginNameAndPw(String loginName, String pw)
    {
        String sql = " select count(1) from t_sys_user where login_name=? and password=? and state=1 ";
        Object[] params = new Object[] {loginName, pw};

        return jdbcTemplate.queryForInt(sql, params);
    }

    @Override
    public int isUrlInUserLimit(String loginName, String url)
    {
        StringBuilder sql = new StringBuilder("");
        sql.append("\n select count(1) from t_sys_menu fi where fi.menu_id in (select menu_id\n ");
        sql.append(" from t_role_menu\n where role_id in\n (select role_id\n from t_user_role ur\n ");
        sql.append(" inner join t_sys_user ui\n on ui.user_id = ur.user_id\n and ui.login_name = ?)) and fi.menu_url = ? ");

        Object[] params = new Object[] {loginName, url};
        int[] types = new int[] {Types.VARCHAR, Types.VARCHAR};

        return jdbcTemplate.queryForInt(sql.toString(), params, types);
    }

    @Override
    public List<Map<String, Object>> queryNaviByUser(String loginName)
    {
        StringBuilder sql = new StringBuilder("");
        sql.append(" \n select menu_id,parent_id,menu_code,menu_bar,menu_url,is_button,menu_icon\n ");
        sql.append(" from t_sys_menu fi\n ");
        sql.append(" where fi.menu_id in\n (select menu_id\n ");
        sql.append(" from t_role_menu\n  where role_id in (select role_id\n ");
        sql.append(" from t_user_role ur\n inner join t_sys_user ui\n ");
        sql.append(" on ui.user_id = ur.user_id\n  and ui.login_name = ?))\n ");
        sql.append(" and fi.is_button = 0 ");

        Object[] params = new Object[] {loginName};
        int[] types = new int[] {Types.VARCHAR};

        return jdbcTemplate.queryForList(sql.toString(), params, types);
    }

    @Override
    public List<String> queryButtonsOfFunc(String loginName, String funcUrl)
    {
        StringBuffer sql = new StringBuffer();
        sql.append(" \n select t.menu_code\n from t_sys_menu t\n ");
        sql.append(" where t.is_button = '1'\n and t.menu_id in\n ");
        sql.append(" (select menu_id\n from t_role_menu\n where role_id in\n ");
        sql.append(" (select role_id\n from t_user_role ur\n inner join t_sys_user ui\n ");
        sql.append(" on ui.user_id = ur.user_id\n and ui.login_name = ?))\n ");
        sql.append(" and instr(t.menu_code, \n (select menu_code \n ");
        sql.append(" from t_sys_menu\n where length(menu_code)=4 and menu_url = ?)) ");

        Object[] paras = new Object[] {loginName, funcUrl};
        int[] types = new int[] {Types.VARCHAR, Types.VARCHAR};

        List<String> buttonFuncNums = jdbcTemplate.queryForList(sql.toString(), paras, types,
            String.class);

        return buttonFuncNums;
    }

}
